package com.hu.hutool.ThreadUtil;

import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;

/**
 * 使用hu-tool的线程池
 *
 * @Author CazCen
 * @Date 2021/11/16 13:03
 */
@Slf4j
public class ThreadUtilTest {

    /**
     * cpu核数
     */
    private static int dealNum = Runtime.getRuntime().availableProcessors();

    private static int NUMBER_REPEAT = 30;

    private static ExecutorService executorService = ThreadUtil.newExecutor(dealNum, dealNum * 2, 1024);


    public static void doCallableB2() {
        CallableB2 b2 = new CallableB2();
        for (int i = 1; i < NUMBER_REPEAT; i++) {
            executorService.submit(b2);
            try {
                // 不使用get的时候，不会阻塞
//                strUser = submitB2.get();
                log.info("strUser i:{}", i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void doRunnableB2() {
        RunnableB1 b1 = new RunnableB1();
        for (int i = 1; i < NUMBER_REPEAT; i++) {
            executorService.submit(b1);
            executorService.execute(b1);
        }
    }

}
